<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect></CategorySelect>
    </el-card>
    <el-card>
        <el-form ref="form" label-width="100px">
            <el-form-item label="SPU名称">
              <el-input placeholder="SPU的名称" value=""></el-input>
            </el-form-item>
          <el-form-item label="品牌">
            <el-select value="请选择品牌" placeholder="SPU的名称">
              <el-option value="华为"></el-option>
              <el-option value="苹果"></el-option>
              <el-option value="小米"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="SPU描述">
            <el-input type="textarea" rows="2" placeholder="描述"></el-input>
          </el-form-item>
          <el-form-item label="SPU图片">
            <el-upload
              action="https://jsonplaceholder.typicode.com/posts/"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove">
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
          </el-form-item>
          <el-form-item label="销售属性">
            <el-select value="请选择品牌" placeholder="SPU的名称">
              <el-option value="价格"></el-option>
              <el-option value="颜色"></el-option>
              <el-option value="重量"></el-option>
            </el-select>
            <el-button type="primary" style="margin: 0px 10px">添加属性</el-button>
          </el-form-item>
          <!--表格组件-->
          <el-table style="width: 100%" border="border" :data="list">
            <el-table-column type="index" label="序号" width="200px" align="center"></el-table-column>
            <el-table-column prop="tmName" label="品牌名称" width="width"></el-table-column>
            <el-table-column prop="prop" label="品牌logo" width="width">
              <template slot-scope="{row,$index}">
                <img :src="row.logoUrl" alt="" style="width: 80px; height: 80px"/>
              </template>
            </el-table-column>
            <el-table-column prop="prop" label="操作" width="width">
              <template slot-scope="{row,$index}">
                <el-button type="warning" icon="el-icon-edit" @click="updateTrademark">修改</el-button>
                <el-button type="danger" icon="el-icon-delete">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!--弹出话框-->
          <el-dialog title="添加/修改订单" :visible.sync="dialogFormVisible">
            <el-form style="width: 80%">
              <el-form-item label="品牌名称" :label-width="elwidth">
                <el-input v-model="form.name" autocomplete="off"></el-input>
              </el-form-item>
            </el-form>
            <el-form style="width: 80%">
              <el-form-item label="品牌logo" :label-width="elwidth">
                <el-upload
                  class="avatar-uploader"
                  action="https://jsonplaceholder.typicode.com/posts/"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload">
                  <img v-if="imageUrl" :src="imageUrl" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                </el-upload>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
            </div>
          </el-dialog>
        </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "spu",
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      // 上传图片的url
      imageUrl:'',
      currentPage: 3,
      elwidth:'100px',
      limit: 5,
      total: 0,
      list: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      formLabelWidth: '120px'
    };
  },
  mounted() {
    // 组件挂载完毕就是发送请求 获取列表数据
    this.getPageList();
  },
  methods: {
    // 获取列表数据的方法
    getPageList() {
      let result = {
        "code": 200,
        "message": "成功",
        "data": {
          "records": [
            {
              "id": 25,
              "tmName": "华硕",
              "logoUrl": "http://39.98.123.211/group1/M00/02/DC/rBHu8mGyMbKAQP0sAAAWKq85SVY068.jpg"
            },
            {
              "id": 26,
              "tmName": "海信",
              "logoUrl": "http://39.98.123.211/group1/M00/02/DC/rBHu8mGyMcCAX4bGAAANH1m3gic067.jpg"
            },
            {
              "id": 27,
              "tmName": "戴尔",
              "logoUrl": "http://39.98.123.211/group1/M00/02/DC/rBHu8mGyMc6AUHGFAAAS6pwrg18359.jpg"
            },
            {
              "id": 32108,
              "tmName": "公关部1231",
              "logoUrl": "http://139.198.127.41:9000/sph/20230714/haier.jpg"
            },
            {
              "id": 32113,
              "tmName": "发到",
              "logoUrl": "http://139.198.127.41:9000/sph/20230713/1.jpg"
            }
          ],
          "total": 46,
          "size": 5,
          "current": 3,
          "searchCount": true,
          "pages": 10
        },
        "ok": true
      }
      if (result.code == 200) {
        this.total = result.data.total;
        this.list = result.data.records;
      }
    },
    addTrademark() {
      this.dialogFormVisible = true;
    },
    updateTrademark(){
      this.dialogFormVisible = true;
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #00afff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
